//
//  LeoMsg.m
//  leo
//
//  Created by 常艳 on 15/9/13.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "LeoMsg.h"
#import "common.h"
@implementation LeoMsg
+(void)showMsg:(NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                    message:msg
                                                   delegate:nil
                                          cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
    alert=nil;
}

+(UIActivityIndicatorView *)showLoadingView
{
//    UIActivityIndicatorView *testActivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
//    float width=[UIScreen mainScreen].bounds.size.width;
//    float height=[UIScreen mainScreen].bounds.size.height;
//    testActivityIndicator.center = CGPointMake(width/2, height/2);//只能设置中心，不能设置大小
    UIActivityIndicatorView *testActivityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    float width=[UIScreen mainScreen].bounds.size.width;
    float height=[UIScreen mainScreen].bounds.size.height;
    testActivityIndicator.center = CGPointMake(width/2, height/2);//只能设置中心，不能设置大小
    [testActivityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [testActivityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [testActivityIndicator setBackgroundColor:[UIColor lightGrayColor]];
    //[testActivityIndicator setFrame = CGRectMack(100, 100, 100, 100)];//不建议这样设置，因为UIActivityIndicatorView是不能改变大小只能改变位置，这样设置得到的结果是控件的中心在（100，100）上，而不是和其他控件的frame一样左上角在（100， 100）长为100，宽为100.
    //[self addSubview:testActivityIndicator];
    //testActivityIndicator.color = [UIColor redColor]; // 改变圈圈的颜色为红色； iOS5引入
    //[testActivityIndicator startAnimating]; // 开始旋转
    //[testActivityIndicator stopAnimating]; // 结束旋转
    //[testActivityIndicator setHidesWhenStopped:YES]; //当旋转结束时隐藏
    return testActivityIndicator;
}

+(void)showToastMsg:(NSString *)msg parentView:(UIView *)parentView showPosition:(enum ToastShowPosition)position hideAfterDelay:(int)second
{
    if([msg isEqualToString:API_REFRESHTOKEN_INVALID_INFO])
        return;
    if(parentView==NULL)
        return;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:parentView animated:YES];
    if(hud==NULL)
        return;
    // Configure for text only and offset down
    hud.mode = MBProgressHUDModeText;
    //hud.labelText = msg;
    hud.detailsLabelText=msg;
    hud.margin = 8.f;
    hud.removeFromSuperViewOnHide = YES;
    switch (position) {
        case TOAST_SHOW_TOP:
            //TODO: need consider TOP toast request.
//            hud.yOffset = -([[UIScreen mainScreen] bounds].size.height/2 - ((44 + 20)* [GlobalConfig getInstance].scaleY));
            break;
        case TOAST_SHOW_MID:
            hud.yOffset = 0.0f;
            break;
        case TOAST_SHOW_END:
            hud.yOffset = 200.0f * [GlobalConfig getInstance].scaleY;
            break;
        default:
            break;
    }
    [hud hide:YES afterDelay:second];
}

+(int)convertToInt:(NSString*)strtemp//判断中英混合的的字符串长度
{
    int strlength = 0;
    char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
    for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
        if (*p) {
            p++;
            strlength++;
        }
        else {
            p++;
        }
        
    }
    return strlength;
}

+(BOOL)isTooLong:(NSString *)testString ChnLength:(int)ChnLength EngLength:(int)EngLength tipsString:(NSString*) tipsString onlyUnderline:(BOOL)onlyUnderline
{
    BOOL hasEng=NO; BOOL hasChn=NO;
    for (int i = 0; i<testString.length; i++) {
        char commitChar = [testString characterAtIndex:i];
        NSString *temp = [testString substringWithRange:NSMakeRange(i,1)];
        const char *u8Temp = [temp UTF8String];
        if (u8Temp!=NULL&&3==strlen(u8Temp)){
            hasChn=YES;NSLog(@"字符串中含有中文");
        }else if((commitChar>64)&&(commitChar<91)){
            hasEng=YES;NSLog(@"字符串中含有大写英文字母");
        }else if((commitChar>96)&&(commitChar<123)){
            hasEng=YES;NSLog(@"字符串中含有小写英文字母");
        }else if((commitChar>47)&&(commitChar<58)){
            hasEng=YES;NSLog(@"字符串中含有数字");
        }else{
            NSLog(@"字符串中含有非法字符");
            //忽略空字符
            if ([temp isEqual:@" "]){}
            else{

            if (![temp isEqualToString:@"_"]||!onlyUnderline){
                [self showMsg:[NSString stringWithFormat:@"%@不合法",tipsString]];
                return YES;}
            }
        }
    }
    
    if (hasEng)
    {
        if ([self convertToInt:testString]>EngLength)
        {
            [self showMsg:[NSString stringWithFormat:@"%@太长，中文字符只能输入%d个，英文字符最多%d个",tipsString,ChnLength/2,EngLength]];return YES;
        }
    }
    if (hasChn)
    {
        if ([self convertToInt:testString]>ChnLength)
        {
            [self showMsg:[NSString stringWithFormat:@"%@太长，中文字符只能输入%d个，英文字符最多%d个",tipsString,ChnLength/2,EngLength]];return YES;
        }
    }

    
    return NO;
}
+(BOOL)ContactPhoneFormatIsWrong:(NSString *)testString{
    int totalNum=0; BOOL wrong=NO;
    for (int i = 0; i<testString.length; i++) {
        char commitChar = [testString characterAtIndex:i];
        NSString *temp = [testString substringWithRange:NSMakeRange(i,1)];
        const char *u8Temp = [temp UTF8String];
        if (3==strlen(u8Temp)){
            NSLog(@"字符串中含有中文");wrong=YES;
        }else if((commitChar>64)&&(commitChar<91)){
            NSLog(@"字符串中含有大写英文字母");wrong=YES;
        }else if((commitChar>96)&&(commitChar<123)){
            NSLog(@"字符串中含有小写英文字母");wrong=YES;
        }else if((commitChar>47)&&(commitChar<58)){
            NSLog(@"字符串中含有数字");totalNum=totalNum+1;
        }else{
            NSLog(@"字符串中含有非法字符");
            if (![temp isEqual:@"+"]){
                if (![temp isEqual:@"-"]){
                wrong=YES;}
            }
        }
    }
    if (totalNum<3){wrong=YES;}
    if (totalNum>16){wrong=YES;}
    if (wrong)
    {
       [self showMsg:[NSString stringWithFormat:@"电话号码不合法,请重新输入"]];
        return YES;
    }
    return NO;
}

@end
